﻿// For an introduction to the Hub template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkID=286574
(function () {
    "use strict";

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;
    var nav = WinJS.Navigation;
    var localSettings = Windows.Storage.ApplicationData.current.localSettings;
    WinJS.Namespace.define("SFTransportation", { GPSDisabled: false });

    app.addEventListener("activated", function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }

            if (app.sessionState.history) {
                nav.history = app.sessionState.history;
            }
            args.setPromise(WinJS.UI.processAll().then(function () {

                var gpsToggleButton = document.getElementById('toggleGPSButton');
                if (gpsToggleButton) {
                    gpsToggleButton.addEventListener("click", toggleGPSButton);
                    if (localSettings.values["gpsDisabled"]) {
                        gpsToggleButton.winControl.selected = false;
                        gpsToggleButton.winControl.label = "GPS Disabled";
                        SFTransportation.GPSDisabled = true;
                    }
                }

                if (nav.location) {
                    nav.history.current.initialPlaceholder = true;
                    return nav.navigate(nav.location, nav.state);
                } else {
                    return nav.navigate(Application.navigator.home);
                }
            }));
        }
    });

    function toggleGPSButton() {
        var toggleButton = document.getElementById('toggleGPSButton').winControl;
        if (SFTransportation.GPSDisabled) {
            SFTransportation.GPSDisabled = false;
            toggleButton.label = "GPS Enabled";
            localSettings.values["gpsDisabled"] = false;
        }
        else {
            SFTransportation.GPSDisabled = true;
            toggleButton.label = "GPS Disabled";
            localSettings.values["gpsDisabled"] = true;
        }
    };

    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. If you need to 
        // complete an asynchronous operation before your application is 
        // suspended, call args.setPromise().
        app.sessionState.history = nav.history;
    };

    app.start();
})();
